//
//  CatgoryDetaiViewController.swift
//  ToDoList
//
//  Created by Qingke on 16/4/14.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

protocol CatgoryDetailDelegate: class {
    func catgoryViewController(controller: CatgoryDetaiViewController,didFinishAddtask category: Category)
    func catgoryViewController(controller: CatgoryDetaiViewController,didFinishEditask category: Category)

}
class CatgoryDetaiViewController: UITableViewController, UITextFieldDelegate,iconPickerDelegate{
    
    @IBOutlet weak var iconNameImage: UIImageView!
    weak var catgoryDelegate: CatgoryDetailDelegate?
    var category:Category?  //2
    var categoryToEdit: Category?
    var iconName = "No Icon"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let categoryToEdit = categoryToEdit {
            self.textFiled.text = categoryToEdit.name
            navigationItem.title = "编辑任务"
            self.iconName = categoryToEdit.iconName
        }
        iconNameImage.image = UIImage(named: iconName)
    }
    
    @IBOutlet weak var textFiled: UITextField!

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    @IBAction func savaButton(sender: AnyObject) {
        if let tc = categoryToEdit {
            tc.name = textFiled.text!
            tc.iconName = iconName
            catgoryDelegate?.catgoryViewController(self, didFinishEditask: tc)
            
        } else {
            let taskCargory = Category(name: textFiled.text!)
            taskCargory.iconName = iconName
            catgoryDelegate?.catgoryViewController(self, didFinishAddtask: taskCargory)
        }
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    @IBAction func cancelButton(sender: UIBarButtonItem) {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func inconPickerViewController(viewcontroller: IconPickerController, iconDidFinishPick icon: String) {
        self.iconName = icon
        iconNameImage.image = UIImage(named: iconName)
        
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as! IconPickerController
        controller.iconDelegate = self
    }
    

}















